package com.woniuxy.yoga.commons.web.log;

import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

/**
 * 日志通知aop
 */
@Aspect
@Component
@Slf4j
public class YogaLogAdvice {

    @Around("@annotation(com.woniuxy.yoga.commons.web.log.YogaLog)")
    public Object log(ProceedingJoinPoint joinPoint) throws Throwable {
        try {
            //输入日志
            MethodSignature signature = (MethodSignature) joinPoint.getSignature();
            Method method = signature.getMethod();
            YogaLog annotation = method.getDeclaredAnnotation(YogaLog.class);
            log.info("============开始执行{}============",annotation.value());
            log.info("参数：{}",JSONUtil.parse(joinPoint.getArgs()).toString());
            Long start = System.currentTimeMillis();
            Object object =  joinPoint.proceed();
            //输出日志
            log.info("返回：{}", JSONUtil.parse(object).toString());
            log.info("耗时：{}",System.currentTimeMillis()-start);
            log.info("============{}结束============",annotation.value());
            return object;
        } catch (Throwable e) {
            throw e;
        }
    }
}
